package com.truongnha.clienttn.data.dao;

import java.util.ArrayList;
import java.util.List;

import com.truongnha.clienttn.data.entity.TeacherSubjectEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TeacherSubjectDAO {
	private final SQLiteDatabase db;

	public TeacherSubjectDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(int isComment, String className, String homeTeacher,
			int classId, String subject, int size, int subjectId) {
		ContentValues inititalValue = new ContentValues();

		inititalValue.put(TeacherSubjectScheme.IS_COMMENT, isComment);
		inititalValue.put(TeacherSubjectScheme.CLASS_NAME, className);
		inititalValue.put(TeacherSubjectScheme.HOME_TEACHER, homeTeacher);
		inititalValue.put(TeacherSubjectScheme.CLASS_ID, classId);
		inititalValue.put(TeacherSubjectScheme.SUBJECT, subject);
		inititalValue.put(TeacherSubjectScheme.SIZE, size);
		inititalValue.put(TeacherSubjectScheme.SUBJECT_ID, subjectId);
		return db.insert(TeacherSubjectScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(TeacherSubjectScheme.TABLE_NAME, null, null);
	}

	public List<TeacherSubjectEntity> getAll() {
		String sql = "SELECT * from " + TeacherSubjectScheme.TABLE_NAME;
		Cursor mcursor = db.rawQuery(sql, null);
		List<TeacherSubjectEntity> list = new ArrayList<TeacherSubjectEntity>();

		if (mcursor.getCount() != 0) {
			mcursor.moveToFirst();
			while (!mcursor.isAfterLast()) {
				TeacherSubjectEntity teacherSubjectEntity = new TeacherSubjectEntity(
						mcursor.getInt(0), mcursor.getInt(1),
						mcursor.getString(2), mcursor.getString(3),
						mcursor.getInt(4), mcursor.getString(5),
						mcursor.getInt(6), mcursor.getInt(7));
				list.add(teacherSubjectEntity);
				mcursor.moveToNext();
			}
		}
		mcursor.close();
		return list;
	}

	public TeacherSubjectEntity getHomeClass() {
		List<TeacherSubjectEntity> list = getAll();
		for (TeacherSubjectEntity teacherSubjectEntity : list) {
			if (teacherSubjectEntity.getSubject().equals("-1"))
				return teacherSubjectEntity;
		}
		return null;
	}
}
